<template>
  <div class="room-list">
    <a-row v-if="dataList && dataList.length" :gutter="18">
      <a-col
        v-for="(item, index) of dataList"
        :key="item.room_id || index"
        :span="8"
        class="room-item-col"
        @click="onClick(item)"
      >
        <corner-border-box
          :id="`room-${item.room_id}`"
          :class="{ 'room-item': true, active: roomId === item.room_id }"
          :color="roomId === item.room_id ? '#3dd8ff' : 'unset'"
          :style="{ cursor: 'pointer', background: item.room_num == 10000 ? 'rgb(8 77 84 / 60%)' : 'none' }"
        >
          <a-row>
            <a-col :span="12" style="border-right: 1px solid rgba(255, 255, 255, 0.15)">
              <div
                class="item-num"
                :style="
                  Object.assign(
                    { color: colorEnum[item.dwelling_state] },
                    item.room_num == 10000 && { fontSize: '16px', lineHeight: '36px' }
                  )
                "
              >
                {{ item.room_num == 10000 ? '服务用房' : item.room_num }}
              </div>
              <div class="item-label">房号</div>
            </a-col>
            <a-col :span="12">
              <div style="height: 30px">
                <span class="item-num" :style="{ color: colorEnum[item.dwelling_state] }">{{ item.people_total }}</span
                ><span v-if="item.dwelling_state == 'D0003_8'">/</span
                ><span v-if="item.dwelling_state == 'D0003_8'" :style="{ color: colorEnum[item.dwelling_state] }">{{
                  item.people_num_max || 0
                }}</span
                >人
              </div>
              <div class="item-label">居住数</div>
            </a-col>
          </a-row>
          <div class="item-img-container">
            <img :src="getImg(item.has_dangyuan == '0' ? 'dang_f' : 'dang_t')" />
            <!--     需求: 2022-7-28 去掉警察和医生       -->
            <!--            <img-->
            <!--              :src="getImg(item.has_police == '0' ? 'jingcha_f' : 'jingcha_t')"-->
            <!--            />-->
            <img :src="getImg(item.has_guanai == '0' ? 'guanai_f' : 'guanai_t')" />
            <!--     需求: 2022-7-28 去掉警察和医生       -->
            <!--            <img-->
            <!--              :src="getImg(item.has_doctor == '0' ? 'yisheng_f' : 'yisheng_t')"-->
            <!--            />-->
            <img :src="getImg(item.has_special == '0' ? 'teshu_f' : 'teshu_t')" />
            <img :src="getImg(item.has_key == '0' ? 'zhongdian_f' : 'zhongdian_t')" />
          </div>
        </corner-border-box>
      </a-col>
    </a-row>
    <empty v-else />
  </div>
</template>

<script>
import Empty from '@gis/lib/components/Empty/index.vue';
import CornerBorderBox from '../../../../../components/corner-border-box.vue';
import areaCodeMixin from '@gis/lib/mixins/areaCodeMixin.js';
import { colorEnum } from '../assets/enum.js';
import { realityHouseRoom } from '@gis/lib/api';
import find from 'lodash-es/find.js';
import findIndex from 'lodash-es/findIndex.js';
import debounce from 'lodash-es/debounce.js';

export default {
  name: 'room-list',
  components: { CornerBorderBox, Empty },
  mixins: [areaCodeMixin],
  props: {
    value: { type: String, default: '' }, //房间id
    cellId: { type: String, default: '' }, //单元id
    dwellingState: { type: String, default: '' }, //房屋类型
    livingType: { type: Array, default: () => [] }, //人员类型
  },
  data() {
    return {
      dataList: [],
      colorEnum,
      roomId: '',
    };
  },
  computed: {
    paramsData() {
      return Object.assign({}, this.apiParams, {
        page_size: 9999999,
        cell_id: this.cellId,
        dwelling_state: this.dwellingState,
        living_type: this.livingType.join(','),
      });
    },
  },
  watch: {
    value: {
      immediate: true,
      handler(roomId) {
        this.roomId = roomId;
      },
    },
    paramsData: {
      immediate: true,
      deep: true,
      handler: debounce(function (params) {
        if (params.cell_id) {
          this.getListDataAsync(params);
        }
      }),
    },
  },
  methods: {
    onClick(data = {}) {
      this.roomId = data.room_id;
      this.$emit('update:modelValue', this.roomId);
      this.$emit('input', this.roomId);
      this.$emit('change', this.roomId);
    },
    getImg(img) {
      return new URL(`../assets/fang_ic_${img}.png`, import.meta.url).href;
    },
    scrollToActiveRoom() {
      this.$nextTick(() => {
        const activeRoom = document.getElementById(`room-${this.roomId}`);
        // console.log(activeRoom);
        if (activeRoom) {
          activeRoom.scrollIntoView({
            behavior: 'smooth',
            block: 'end',
            inline: 'nearest',
          });
        }
      });
    },
    async getListDataAsync(params) {
      if (!params.cell_id) return;
      const res = await realityHouseRoom(params);
      const data = res ? res.data || {} : {};
      const list = data.list || [];
      // 测试代码
      // if (list.length > 5) {
      //   list[3].room_num = 10000;
      // }
      const index = findIndex(list, (item) => item.room_num == 10000);
      this.dataList = index > -1 ? list.splice(index, 1).concat(list) : list;
      const activeRoom = find(this.dataList, (item) => item.room_id === this.roomId);
      this.onClick(activeRoom || this.dataList[0]);
      this.scrollToActiveRoom();
    },
  },
};
</script>

<style scoped lang="less">
.room-list {
  width: 100%;
  height: 100%;
  padding: 0 15px;
  overflow: hidden auto;
  color: #f0f0f0;
}
.room-item-col {
  text-align: center;
  margin-bottom: 10px;
  .room-item {
    &.active {
      border-color: #3dd8ff;
    }
  }
  .item-num {
    height: 30px;
    font-size: 24px;
    font-weight: bold;
  }
  .item-label {
    margin-bottom: 5px;
  }
  .item-text {
    font-size: 14px;
  }
  .item-img-container {
    border-top: 1px solid rgba(255, 255, 255, 0.15);
    display: flex;
    justify-content: space-around;
    padding: 5px 0;
  }
}
</style>
